<?php

namespace app\forms\item;

use yii\db\Exception;
use app\forms\Model;
use app\models\Item;
use app\models\Role;
use yii\web\HttpException;
use app\validators\PositiveInteger;

class RoleForm extends Model
{
    public $id;
    public $name;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],
            ['id', 'required'],
            ['id', 'exist', 'targetClass' => Item::class],

            ['name', 'trim'],
            ['name', 'string'],
        ];
    }

    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function getRole($param)
    {
        $form = new self;
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';

        try {
            return (new Role())->getListByItem($param);
        } catch (Exception $e) {
            $form->serverError();
        }
    }
}